#include <iostream>
#include <string>
#include <cmath>
#include <algorithm>
using namespace std;

#define MAX 20

int n;
int ans[MAX];
int x[MAX];      // 暂存选择数
bool chosed[26]; // 记录一个字母是否被选中
string choices;  // 可选字符
bool found = false;

bool Solution()
{
    if (x[0] - (int)pow(x[1], 2) + (int)pow(x[2], 3) - (int)pow(x[3], 4) + (int)pow(x[4], 5) == n)
        return true;
    return false;
}

void f(int depth)
{
    if(found)
      return;

    if (depth > 5)
    {
        if (Solution())
        {
            found = true;
            for (int i = 0; i < 5; i++)
            {
              ans[i] = x[i];
            }
        }
    }
    else
    {
        for (char ch : choices)
        {
            if (!chosed[ch - 'A'])
            {
                int val = (int)(ch - 64);
                x[depth] = val;
                chosed[ch - 'A'] = true;
                f(depth + 1);
                chosed[ch - 'A'] = false;
            }
        }
    }
}

int main()
{
    found = false;
    cout << "请输入一个整数n，与一个不同大写字母组成的字符串" << endl;
    cin >> n >> choices;
    std::sort(choices.begin(),choices.end(),greater<char>());
    f(0);
    if (!found)
        cout << "no solution" << endl;
    else
    {
        cout << "字典序最大的密码是：";
        for (int i = 0; i < 5; i++)
            cout << (char)(ans[i] + 64);
        cout << endl;
    }
    return 0;
}

/*
1 ABCDEFGHIJKL
11700519 ZAYEXIWOVU
3072997 SOUGHT
1234567 THEQUICKFROG
*/
